{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Minimal Example pandapipes\n",
    "\n",
    "## Creating a low pressure gas network\n",
    "\n",
    "We consider the following simple 3-junction example network with an ideal valve as a minimal example:\n",
    "\n",
    "<img src=\"pics/minimal_example.png\">\n",
    "\n",
    "The above network can be created in pandapipes as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandapipes as pp\n",
    "\n",
    "#create empty net\n",
    "net = pp.create_empty_network(fluid=\"lgas\")\n",
    "\n",
    "# create junction\n",
    "j1 = pp.create_junction(net, pn_bar=1.05, tfluid_k=293.15, name=\"Junction 1\")\n",
    "j2 = pp.create_junction(net, pn_bar=1.05, tfluid_k=293.15, name=\"Junction 2\")    \n",
    "j3 = pp.create_junction(net, pn_bar=1.05, tfluid_k=293.15, name=\"Junction 3\")    \n",
    "\n",
    "# create junction elements\n",
    "ext_grid = pp.create_ext_grid(net, junction=j1, p_bar=1.1, t_k=293.15, name=\"Grid Connection\")\n",
    "sink = pp.create_sink(net, junction=j3, mdot_kg_per_s=0.045, name=\"Sink\")\n",
    "\n",
    "# create branch element\n",
    "pipe = pp.create_pipe_from_parameters(net, from_junction=j1, to_junction=j2, length_km=0.1, diameter_m=0.05, name=\"Pipe 1\")\n",
    "valve = pp.create_valve(net, from_junction=j2, to_junction=j3, diameter_m=0.05, opened=True, name=\"Valve 1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the fluid used here is ``lgas`` (low-calorific natural gas). You can find 7 predefined fluids in pandapipes:\n",
    "- ``lgas`` (low-calorific natural gas)\n",
    "- ``hgas`` (high-calorific natural gas)\n",
    "- ``hydrogen``\n",
    "- ``water``\n",
    "- ``biomethane_pure``\n",
    "- ``biomethane_treated`` (see [here](https://pandapipes.readthedocs.io/en/latest/fluid_properties/fluids.html) for the composition)\n",
    "- ``air``\n",
    "\n",
    "And that the predefined valve element is an ideal valve. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data Structure\n",
    "\n",
    "Each dataframe in a pandapipes net object contains the information about one pandapipes element, such as pipe, sink, valve etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.junction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.pipe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.ext_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pipe Flow\n",
    "\n",
    "We now run a pipe flow:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp.pipeflow(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And check out at the results for junctions and pipes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_junction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "They're is no pressure loss between junction 2 and junction 3 because of the ideal valve."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_pipe"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Closed valve\n",
    "\n",
    "We now close the valve between junction 2 and junction 3:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Problem can not run pipeflow if valve closed\n",
    "net.valve.opened = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The closed valve cuts the sink from the external grid:\n",
    "\n",
    "<img src=\"pics/minimal_example_closed_valve.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This can be verified by running a power flow and inspecting the results. The pressure and temperature at junction 2 is given as NaN:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pp.pipeflow(net)\n",
    "net.res_junction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Also the results from the pipe show that the mass flow is almost zero and the speed of the mass flow is zero. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_pipe"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
